<?php
/**
 * Created by PhpStorm.
 * User: 13790
 * Date: 2018/3/22
 * Time: 14:50
 */

namespace app\admin\controller;


use think\Exception;
use Think\Model;

class Auth extends Base
{
    public function lst(){
        $data=model('Auth')->sel();

        return view('',['data'=>$data]);
    }
    public function add(){
        if(request()->isPost()){
            try{
                $res = model('Auth')->addData();
                if($res !== true){
                    throw new \Exception($res);
                }
            }catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('添加成功','Index/index');
        }
        //查询所有的上级权限
        $data = model('Auth')->order('auth_ids asc')->select();
        return view('',['data'=>$data]);
    }
    public function del($id){

        $m = db('Auth');
        $num_rows = $m->where("id={$id}")->delete();
        if($num_rows>0){
            $url = U('Index/index');
            $this->success('删除成功！',"{$url}");
        }else{
            $this->error('删除失败！');
        }
    }
    public function edit(){
        if(request()->isPost()){
            try{
                $res = model('Auth')->editData();
                if($res !== true){
                    exception($res);
                }
            }catch (\Exception $e){
                return $this->error($e->getMessage());
            }
            return $this->success('编辑成功','lst');
        }
        //查询权限对应的数据
        $id = input('param.id');
        $data = model('Auth')->find($id);

        //查询所有的权限
        $auth_data = model('Auth')->order('auth_ids')->select();
        return view('',['data'=>$data,'auth_data'=>$auth_data]);
    }
}